package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.CommonPreoblemContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DetailsWebViewActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/5
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class CommonProblemPresenter {
    public CommonPreoblemContract commonPreoblemContract;
    public AppCompatActivity me;

    public CommonProblemPresenter(CommonPreoblemContract commonPreoblemContract, AppCompatActivity me) {
        this.me = me;
        this.commonPreoblemContract = commonPreoblemContract;
    }

    public void getProblemList(String token, final int page) {
        Model.getInstance()
                .getUserApi()
                .getBlemList(token, page)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        if (list == null) {
                            ToastUtil.show(map.get("message"));
                            return;
                        }
                        commonPreoblemContract.Success(list);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    public void getProblemInfo(String token, String id) {
        Model.getInstance()
                .getUserApi()
                .getProblemInfo(token, id)
                .enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Map<String, String> map = Constants.codeGet(me, response);
                if (map == null) {
                    return;
                }
                if (map.get("data") == null) {
                    ToastUtil.show("此问题没有详情");
                    return;
                }
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                DetailsWebViewActivity.start(me, 5, data.get("title"), data.get("content"));
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {

            }
        });
    }
}